<template>
  <div class="needKnow"> 
    <div class="centerBox">
      <h1 class="title">{{title}}</h1>
      <ul class="list">
        <li class="list-item" v-for="item in list" :key="item.id">{{item}}</li>
      </ul>
      <button class="button" @click="changeShowTips">{{button}}</button>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
// 引入这个页面需要的方法
import { mapMutations, mapGetters } from 'vuex'
import { addOrder,cancelOrder } from 'api/api'
import Wilddog from 'wilddog'
import axios from 'axios'
export default {
  data () {
    return {
      title: '出行须知',
      list: ['严禁携带宠物、活禽等上车', '严禁携带宠物、活禽等上车', '严禁携带宠物、活禽等上车', '严禁携带宠物、活禽等上车', '严禁携带宠物、活禽等上车'],
      button: '确定',
      backCuradress: '',
      HH:'0',
      mm:'0',
      ss:'0',
      str:'0'
    }
  },
  computed: {
    ...mapGetters([
      'carType', // 选择的车的类型的ID
      'flightInfo', // 航班信息
      'timeInfo', // 选择的时间
      'searchBoxInfo', // 选择的上车地点
      'destination', // 选择的下车地点
      'orderName', // 乘车人的姓名
      'orderPhone', // 乘车人的电话
      'carStartTime', // 上车时间
      'startCoor', // 开始坐标
      'endCoor', // 结束坐标
      'publicToken', // 公共token
      'userId', // 用户id
      'carStartTime',
      'waitTime',
      'nowtimes'
    ])
  },
  methods: {
    // 点击使用户须知关闭
    changeShowTips () {
      const _this = this
      let userid = localStorage.getItem('userId')
      let startzb = localStorage.getItem('lat1') + ':' + localStorage.getItem('lng1')
      let endzb = localStorage.getItem('lat2') + ':' + localStorage.getItem('lng2')
      axios.post(addOrder, {
        'param': {
          'fields': {
            'carType': localStorage.getItem('carType'),
            'customerPhone': this.orderPhone,
            'customerName': this.orderName,
            'desAddress': this.destination,
            'desLocation': endzb,
            'flightId': this.flightInfo,
            'orderType': localStorage.getItem('pickType'),
            'passengerId': userid,
            'startAddress': this.searchBoxInfo,
            'startLocation': startzb,
            'planTime': this.carStartTime,
            'useSeats': 2,
            'canWait': 1,
            'token': localStorage.getItem('token')
          }
        }
      }).then((res) => {
        if (res.data.code === 200) {
          // 这个是下单时间
          var timer = setInterval( () => {
            console.log('进入计时')
            this.str = "";
            if(++this.ss==60)
            {
              if(++this.mm==60)
              {
                this.HH++;
                this.mm=0;
              }
              this.ss=0;
            }
            this.str+=this.HH<10?"0"+this.HH:this.HH;
            this.str+=":";
            this.str+=this.mm<10?"0"+this.mm:this.mm;
            this.str+=":";
            this.str+=this.ss<10?"0"+this.ss:this.ss;
            console.log(this.str)
            this.setWaitTime(this.str)
            if(this.str==='00:20:00'){
              clearInterval(timer)
              this.cancelOrder()
            }
            if(localStorage.getItem('orderStatus')==='0'){
              clearInterval(timer)
              localStorage.setItem('orderStatus','1')
            }
          },1000)
          localStorage.setItem("token", res.data.access_token);
          let msg = ''
          let vm = this
          this.setOrderName('')
          this.setOrderPhone('')
          this.setShowTips(false)
          this.setShowCarpool(false)
          this.setShowInType(true)
          this.setShowCancel(true)
          vm.setFlightInfo(msg)
          vm.setTimeInfo(msg)
          vm.setSearchBoxInfo(msg)
          vm.setDestination(msg)
          localStorage.setItem('orderId', res.data.result.id)
          localStorage.setItem('orderNum', res.data.result.orderNum)
          let orderNum = localStorage.getItem('orderNum')
          console.log(orderNum + '订单号')
          let config = {
            uthDomain: 'wd2133607989gbwvql.wilddog.com',
            syncURL: 'https://wd2133607989gbwvql.wilddogio.com/WilddogLocation/order/' + orderNum
          }
          Wilddog.initializeApp(config)
          let orderRes = Wilddog.sync().ref()
          console.log(orderRes + '野狗')
          orderRes.child('resource').on('value', function (snapshot) {
            console.log(snapshot.val())
            var newPost = snapshot.val()
            console.log('订单状态:' + newPost.orderStatus ? newPost.orderStatus : '')
            if (newPost.orderStatus === 2) {
              console.log('等待接单')
            }
            if (newPost.orderStatus === 3) {
              console.log('司机已接单')
              vm.setOrderName('')
              vm.setOrderPhone('')
              vm.setShowCancel(false)
              vm.setShowSuccess(true)
              vm.setShowCancel(false)
              vm.setDriverName(newPost.driverName)
              vm.setDriverPhone(newPost.driverPhone)
              vm.setCarModel(newPost.model)
              vm.setCarNum(newPost.numberPlate)
              vm.setRates(newPost.rates)
              vm.setRated(newPost.rated)
              vm.setShowNoCall(false)   // 接单时如果无应答界面打开就关掉
              let driverId = newPost.driverId
              localStorage.setItem('driverId', driverId)
              // 监听司机id
              let config = {
                uthDomain: 'wd2133607989gbwvql.wilddog.com',
                syncURL: 'https://wd2133607989gbwvql.wilddogio.com/WilddogLocation/map/' + driverId
              }
              Wilddog.initializeApp(config)
              let res = Wilddog.sync().ref()
              res.child('location').on('value', function (snapshot) {
                _this.backCuradress = snapshot.val()
                console.log(_this.backCuradress)
              })
            }
            if (newPost.orderStatus === 4) {
              console.log('司机到达上车地点')
              vm.setShowSuccess(false)
              vm.setShowDriver(true)
            }
            if (newPost.orderStatus === 5) {
              console.log('开始行程')
            }
            if (newPost.orderStatus === 6) {
              console.log('结束行程')
              vm.setShowDriver(false)
              vm.$router.push('/costInfo')
            }
          })
        } else {
          this.$createDialog({
            type: 'alert',
            title: '',
            content: res.data.message,
            icon: 'cubeic-alert',
            onConfirm: function () {
              _this.setShowTips(false)
              location.reload(true) 
            }
          }).show()
        }
      })
    },
    cancelOrder () {
      const _this = this
      let order = localStorage.getItem('orderNum')
      axios.post(cancelOrder, {
        'param': {
          'fields': {
            'orderNum': order,
            'nullRates': '1.2'
          }
        }
      }).then((res) => {
        console.log(res)
        let msg = ''
        if(res.data.code == 200) {
          localStorage.setItem('orderNum', '')
          this.$createToast({ time:700, type: 'warn', txt: res.data.message }).show()
          setTimeout(function () {
            _this.setShowTips(false)
            _this.setShowCarpool(false)
            _this.setShowInType(false)
            _this.setShowCancel(false)
            _this.setShowNoCall(false)
            _this.setShowType(true)
            _this.setShowFlightInfo(msg)
            _this.setShowTimeInfo(msg)
            _this.setShowSearchBox(msg)
            _this.setDestination(msg)
          }, 700)
        }else if (res.data.code == 10086) {
            this.$router.push({path:"/index?"})
            window.location.reload()
        }
      })
    },
    ...mapMutations({
      setShowTips: 'SET_SHOWTIPS',
      setShowCarpool: 'SET_SHOWCARPOOL',
      setShowCancel: 'SET_SHOWCANCEL',
      setFlightInfo: 'SET_FLIGHTINFO',
      setTimeInfo: 'SET_TIMEINFO',
      setSearchBoxInfo: 'SET_SEARCHBOXINFO',
      setDestination: 'SET_DESTINATION',
      setShowInType: 'SET_SHOWINTYPE',
      setCarStartTime: 'SET_CARSTARTTIME',
      setShowSuccess: 'SET_SHOWSUCCESS',
      setShowDriver: 'SET_SHOWDRIVER',
      setDriverName: 'SET_DRIVERNAME',
      setDriverPhone: 'SET_DRIVERPHONE',
      setCarModel: 'SET_CARMODEL',
      setCarNum: 'SET_CARNUM',
      setRates: 'SET_RATES',
      setRated: 'SET_RATED',
      setOrderName: 'SET_ORDERNAME',
      setOrderPhone: 'SET_ORDERPHONE',
      setWaitTime: 'SET_WAITTIME',
      setShowNoCall: 'SET_SHOWNOCALL',
      setShowType: 'SET_SHOWTYPE',
      setShowFlightInfo: 'SET_SHOWFLIGHTINFO',
      setShowTimeInfo: 'SET_SHOWTIMEINFO',
      setShowSearchBox: 'SET_SHOWSEARCHBOX',
      setNowtimes: 'SET_NOWTIMES'
    })
  }
}
</script>

<style  scoped lang="stylus" rel="stylesheet/stylus">
.needKnow
  position: fixed
  top: 0
  bottom: 0
  left: 0
  right: 0
  width: 100%
  height: 100%
  background-color: rgba(153, 153, 153, .4)
  .centerBox
    position: absolute
    top: 50%
    left: 50%
    margin-top: -167px
    margin-left: -140px
    width: 280px
    height: 335px
    background-color: #fff
    .title
      font-weight: 600
      font-size 18px
      margin-top: 30px
    .list
      width: 180px
      height: auto
      margin: 0 auto
      margin-top: 27px
      .list-item
        list-style: inherit
        margin-bottom: 15px
        font-size: 14px
    .button
      position: absolute
      bottom: 10px
      left: 50%
      margin-left: -60px
      width: 120px
      height: 34px
      color: #fff
      background-color: #333
      border-radius: 5px
      border: none
      outline: none
</style>